package laborum2011.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import laborum2011.dao.ContactoDAO;
import laborum2011.dao.EmpresaDAO;
import laborum2011.excepcion.DAOExcepcion;
import laborum2011.modelo.Contacto;
import laborum2011.modelo.Empresa;

/**
 * Servlet implementation class EmpresaServletEliminar
 */
@WebServlet("/EmpresaServletEliminar")
public class EmpresaServletEliminar extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EmpresaServletEliminar() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd;
		HttpSession session = request.getSession();
		
		ContactoDAO vContactoDao = new ContactoDAO();
		Contacto co = new Contacto();
		EmpresaDAO vEmpresaDao = new EmpresaDAO();
		Empresa em = new Empresa();
		
	 	em = (Empresa) session.getAttribute("empresa");
	 	co = (Contacto) session.getAttribute("contacto");
		
	 	try {
			vContactoDao.eliminar(co.getId_contacto());
			vEmpresaDao.eliminaridEmpresa(em.getId_empresa());
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rd = request.getRequestDispatcher("mastera.jsp");
		rd.forward(request, response);
		
	}

}
